﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.BasicSet.RelayType;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using System.Linq.Expressions;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
namespace WaterRevenueMS.WebUI.Controllers
{

    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class RelayTypeController : AdminController
    {

        [Import]
        private IRelayTypeService RelayTypeService { get; set; }


        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new RelayTypeModel();
            return View(model);
        }
        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = RelayTypeService.RelayTypes.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = RelayTypeService.RelayTypes.Where(expr).Select(t => new RelayTypeModel
            {
                Id = t.Id,
                Zhong_JLX=t.Zhong_JLX,
                DefualtValue=t.DefaultValue,
                OrderNo=t.OrderNo,
                Zhong_JXY=t.Zhong_JXY

            }).OrderBy(t => t.OrderNo).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.Zhong_JLX,
                            c.Zhong_JXY.ToString(),  
                            c.OrderNo.ToString(),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            var model = new RelayTypeModel();

            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(RelayTypeModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = RelayTypeService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new RelayTypeModel();
            var entity = RelayTypeService.RelayTypes.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new RelayTypeModel
                {
                    Zhong_JLX=entity.Zhong_JLX,
                    Zhong_JXY=entity.Zhong_JXY,
                    OrderNo = entity.OrderNo
                };
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(RelayTypeModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = RelayTypeService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = RelayTypeService.Delete(Id);
            return Json(result);
        }


        private Expression<Func<RelayType, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<RelayType> bulider = new DynamicLambda<RelayType>();
            Expression<Func<RelayType, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Zhong_JLX"]))
            {
                var data = Request["Zhong_JLX"].Trim();
                Expression<Func<RelayType, Boolean>> tmp = t => t.Zhong_JLX.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            Expression<Func<RelayType, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }


    }
}
  